﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Net;

namespace CMSE.Extensions.Vimeo
{
    public class InformationExtractor
    {
        private const string VIDEO_URL = "http://vimeo.com/api/v2/video/{0}.xml";

        public InformationExtractor() { }

        public VimeoVideo GetInformation(Uri videoUrl){


            string videoToken = GetVideoToken(videoUrl);

            string apiUrl = string.Format(VIDEO_URL, videoToken);

            WebClient webClient = new WebClient();
            string content = webClient.DownloadString(apiUrl);

            VimeoVideo video = new VimeoVideo(content);
            return video;

        }

        public string GetInformationStream(Uri videoUrl)
        {

            string videoToken = GetVideoToken(videoUrl);

            string apiUrl = string.Format(VIDEO_URL, videoToken);

            WebClient webClient = new WebClient();
            string content = webClient.DownloadString(apiUrl);

            return content;

        }

        private string GetVideoToken(Uri videoUrl)
        {
            return videoUrl.Segments[videoUrl.Segments.Length-1];
        }

        internal string GetInformationStream(string url)
        {
            Uri uri = new Uri(url);
            return GetInformationStream(uri);
        }
    }
}
